#!/bin/bash
#绿色解压、修改内容
#1，安装git工具
#2，下载、安装


#####需求替换
TOOLS="git"
SOFT="tomcat"
VER="8"
#VER2="1.8.0"
SER="tomcat"
#RPMNAME="tomcat"




#####无需替换
GITEXT=".git"
FILEEXT="_init.sh"

TAG="."
TAG1=".*"
NUM="[0-9]"


SHELLDIR="/opt/shell/"
SOFTDIR="/opt/"$SOFT"-"$VER"/"

GITADDR="https://gitee.com/ffing/"
GITDOWN=""$GITADDR""$SOFT"-"$VER""$GITEXT""

LOCALDIR="/usr/local/"

IPLAN=$(hostname -I | awk -F " " '{printf $1}')
IPWAN=$(curl http://pv.sohu.com/cityjson 2>> /dev/null | awk -F '"' '{print $4}')


#####安装TOOLS类
pName=$(rpm -qa | grep "^${TOOLS}${TAG}${NUM}${TAG}${NUM}")
if [ $? -eq 0 ]
then
       echo -e	"----------已存在，无需安装----------\n"${pName}""
	   echo
else
echo "----------正在安装 "${TOOLS}"……"
yum -y install $TOOLS
               if [ $? -eq 0 ]
               then
                        echo "----------安装完成 "${TOOLS}""
						echo
               else
                        echo -e "\e[41m >>>错误>>>安装失败 "${TOOLS}"\e[0m"
						echo
               fi
fi

#####检测java
java -version 2>/dev/null
if [ $? -ne 0  ];then
echo "开始安装JAVA"
bash <(curl https://gitee.com/ffing/onekey/raw/master/java-8_init.sh)
fi



#####下载、安装

#版本号类型
pName=$(rpm -qa | grep "^${SOFT}${TAG}${NUM}${TAG}${NUM}")
#名称类型
#pName=$(rpm -qa | grep "^${SOFT}${TAG1}${RPMNAME}")
if [ $? -eq 0 ]
then
	   echo -e "\e[31m !!!!!!  安装失败  !!!!!! \e[0m"
       echo -e	"----------已存在，请先卸载----------\n"${pName}""
	   echo
else
		echo
		echo "----------开始下载 "${SOFT}"……"
		if [ ! -d $SOFTDIR ];then
		echo "创建${SOFT}下载目录 ${SOFTDIR}"
		git clone $GITDOWN $SOFTDIR 
		
		else
		echo "存在${SOFT}下载目录 ${SOFTDIR}"
		fi
		
		echo "----------Copy "${SOFT}" 到 /usr/local/……"
		\cp -r ${SOFTDIR} ${LOCALDIR}

				echo "----------添加"${SOFT}"环境变量----------"
				cat /etc/profile |grep TOMCAT_HOME= 
       		 if [ $? -eq 0 ]
       		 then
      		  #修改JAVA全局变量
				sed -i 's/TOMCAT_HOME=.*/TOMCAT_HOME=\/usr\/local\/tomcat-8/g' /etc/profile
				sed -i 's/CATALINA_HOME=.*/CATALINA_HOME=\/usr\/local\/tomcat-8/g' /etc/profile
				sed -i 's/CATLINA_BASE=.*/CATLINA_BASE=\/usr\/local\/tomcat-8/g' /etc/profile
				else
				#插入JAVA全局变量
				cat ${SOFTDIR}export >> /etc/profile
				fi
				source /etc/profile

				#配置systemctl
				echo "添加systemd服务:$SER"
				cat ${SOFTDIR}tomcat.service > /usr/lib/systemd/system/tomcat.service
				systemctl daemon-reload
				
				#防火墙放行8080
				echo "防火墙放行端口：8080"
				sed -i "/\/zone/i     <port protocol=\"tcp\" port=\"8080\"\/>" /etc/firewalld/zones/public.xml
				firewall-cmd --reload > /dev/null 2>&1

				#重启
				systemctl restart $SER > /dev/null 2>&1
				echo "添加开机启动：$SER"
				systemctl enable $SER   > /dev/null 2>&1


				#####服务状态

				
				ps -ef|grep $SER|grep -vE "grep|$$" >/dev/null
				if [ $? -eq 0 ];then
				echo
				echo "----------JAVA 版本信息----------"
      			java -version
      			echo 
      			echo -e "\e[42m "${SER}" 			已经启动\e[0m"
				echo
      			echo "  启动：systemctl start $SER"
				echo "  停止：systemctl stop $SER"
				echo "  重启：systemctl restart $SER"
      			echo "  Tomcat内网访问：http://"$IPLAN":8080"
				echo "  Tomcat外网访问：http://"$IPWAN":8080"
				echo '  Tomcat安装目录 /usr/local/tomcat-8'
				else
				echo -e "\e[41m "${SER}" 			启动失败\e[0m"
				fi
fi


